package cn.jizi.bshisy.controller;

import cn.jizi.bshisy.entity.Bed;
import cn.jizi.bshisy.service.BedService;
import cn.jizi.bshisy.tool.PageSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 李鹏辉Code
 * @create 2020 -11 -04 9:35
 */
@RestController
@RequestMapping("/bed")
public class BedController {

    @Autowired
    private BedService bedService;

    @ApiOperation(value = "新病人入院开床", notes = "新病人入院开床")
    @PostMapping(value = "/newPatientOpen")
    public String newPatientOpen(Bed bed) {
        if (bedService.newPatientOpen(bed))
            return "新病人入院开床成功";
        return "新病人入院开床失败";
    }

    @ApiOperation(value = "更改病床信息", notes = "根据iid更改病床信息")
    @PutMapping(value = "/changeBedInformation")
    public String changeBedInformation(Bed bed) {
        if (bedService.changeBedInformation(bed))
            return "更改病床信息成功";
        return "更改病床信息失败";
    }

    @ApiOperation(value = "根据iid或名字查询病床信息", notes = "根据iid或名字查询病床信息")
    @GetMapping(value = "/queryPatientBedInformation")
    public List<Bed> queryPatientBedInformation(Integer iid, String mname, String PageIndex,
                                                Integer pageSize, Map<String, Object> map) {
        PageSupport pages = new PageSupport();
        List<Bed> bedList = null;
        //设置页面容量
        if (pageSize == null || pageSize.equals("")) {
            pageSize = 3;
        }
        //当前页码 默认第一页
        int currentPageNo = pages.getCurrentPageNo();
        if (PageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(PageIndex);
            } catch (NumberFormatException e) {
//                return "页面";
            }
        }
        List<Bed> bedsCount = bedService.queryConditionPaginationCount(iid, mname);
        //总页数
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(bedsCount.size());
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页dsa
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        bedList = bedService.queryConditionPagination(iid, mname, (currentPageNo - 1) * pageSize, pageSize);
        map.put("pages", pages); //分页数据
        map.put("bedList", bedList); //分页数据
        return bedList;
    }
}